package it.ap.sa.acqua.plant.web;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import it.ap.sa.acqua.dao.beans.SearchPlantFilter;
import it.ap.sa.acqua.dao.beans.TPlantBean;
import it.ap.sa.acqua.plant.service.IPlantService;
import it.ap.sa.acqua.shared.web.AbstractCtrl;
import it.ap.sa.core.dao.beans.search.ResponseData;
import it.ap.sa.core.utils.LogUtil;

/**
 * Controller per la gestione delle piante
 * 
 * @author Alessandro Paolillo
 *
 */
@Controller(value = "Plant Controller")
@RequestMapping("/plantCtrl")
public class PlantController extends AbstractCtrl {

	private static final Logger logger = LoggerFactory.getLogger(PlantController.class.getName());
	
	@Autowired
	private IPlantService plantService;
	
	@Override
	@RequestMapping(value = "/plantDetail")
	public ModelAndView loadPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathPlantPages() + "plantDetail");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	@RequestMapping(value = "/plantPrefer")
	public ModelAndView loadPreferPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadPreferPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathPlantPages() + "plantPrefer");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	@RequestMapping(value = "/myPlant")
	public ModelAndView loadMyPlantPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadMyPlantPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathPlantPages() + "myPlant");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	/**
	 */
	@RequestMapping(value = "/getPlant.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getPlant(HttpServletRequest request, @RequestParam(name="id") Long id) throws Exception {
		
		String    idLog     = "getPlant";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			TPlantBean bean = plantService.getPlant( id );
			result = this.getResponseDataByObject(bean, locale);
		}catch(Exception e){
			logger.error("Error in getPlant", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getPlants.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getPlants(HttpServletRequest request) throws Exception {
		
		String    idLog     = "getPlants";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TPlantBean> listBean = plantService.getPlants();
			result = this.getResponseDataByList(listBean, locale);
		}catch(Exception e){
			logger.error("Error in getPlants", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/searchPlant.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData searchPlant(HttpServletRequest request, 
			@RequestBody SearchPlantFilter searchPlantFilter ) throws Exception {
		
		String    idLog     = "searchPlant";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TPlantBean> resultData = plantService.searchPlant( searchPlantFilter );
			result = this.getResponseDataByList(resultData, locale);
			
			if ( searchPlantFilter.isPaginate() ) {
				Long iTotalRecords = plantService.searchCountPlant( searchPlantFilter );
				result.setiTotalRecords( iTotalRecords.intValue() );
			}
		}catch(Exception e){
			logger.error("Error in searchPlant", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
}
